package com.augurit.common.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.lob.SerializableClob;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springside.modules.orm.Page;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.entity.SysMetaTable;
import com.augurit.common.sysmeta.convert.SysMetaTableConvertor;
import com.augurit.common.sysmeta.web.form.SysMetaTableForm;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.smartform.constant.PageConstant;
import com.augurit.smartform.database.Table;
import com.augurit.smartform.database.dialect.Dialect;
import com.augurit.smartform.factory.DialectFactory;

@Repository
public class SysMetaTableDao extends BaseDao<SysMetaTable, Long>{
	
	/**
	 * 获取form对象
	 * @param id
	 * @return
	 */
	public SysMetaTableForm getForm(Long id){
		SysMetaTableForm form = null;
		if(id != null){
			SysMetaTable entity = this.get(id);
			form = SysMetaTableConvertor.convertToForm(entity);
		}
		return form;
	}
	
	/**
	 * 保存或者修改form对象
	 *  @param id
	 */
	public void save(SysMetaTableForm form){
		if(form != null){
			SysMetaTable entity = null;
			
			//准备VO对象
			if(form.getId() != null)
				entity = this.get(form.getId());
			else
				entity = new SysMetaTable();
			
			//属性值转换
			SysMetaTableConvertor.convertToEntity(form,entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
			
		}
	}
	
	/**
	 * 根据用户获取表结构定义form对象列表
	 * @param owner
	 * @return
	 */
	public List<SysMetaTableForm> getTablesByOwner(String owner){
		List<SysMetaTableForm> forms = null;
		if(owner != null){
			String hql =  "from SysMetaTable ps where ps.owner='" + owner +"'";
			List<SysMetaTable> list = this.find(hql);
			if(list != null)
				forms = SysMetaTableConvertor.convertEntityListToFormList(list);
		}
		return forms;
	}
	
	/**
	 * 获取所有的用户
	 * @return
	 */
	public List<String> getAllOwner(){
		String hql =  "select distinct ps.owner from SysMetaTable ps";
		List<String> list = this.find(hql);
		return list;
	}
	
	/**
	 * 根据实体名数组获取Id数组
	 */
	public Long[] getTableIdsByEntityNames(String entityNames[]){
		if(entityNames != null && entityNames.length>0){
			StringBuffer sb = new StringBuffer("select ps.id from SysMetaTable ps where 1=2");
			for(String entityName:entityNames){
				sb.append(" or ps.entityName ='" +entityName + "'");
			}
			
			List<Long> ids = this.find(sb.toString());
			if(ids != null && ids.size()>0){
				return ids.toArray(new Long[ids.size()]);
			}
		}
		return null;
	}
	
	/**
	 * 根据实体名数组获取Id数组
	 */
	public Long[] getTableIdsByTableNames(String tableNames[]){
		if(tableNames != null && tableNames.length>0){
			StringBuffer sb = new StringBuffer("select ps.id from SysMetaTable ps where 1=2");
			for(String tableName:tableNames){
				sb.append(" or ps.tableName ='" +tableName + "'");
			}
			
			List<Long> ids = this.find(sb.toString());
			if(ids != null && ids.size()>0){
				return ids.toArray(new Long[ids.size()]);
			}
		}
		return null;
	}
	
	/**
	 * 根据名称从数据库中查询最新的表单
	 * @param name
	 * @return
	 * cuigeqiang
	 */
	public SysMetaTable getLastedSmFormByName(String name){
		SysMetaTable lastedSmForm=null;
		
		String hql="from SysMetaTable t where t.formName=? order by t.version desc";
//		Query query=this.getSession().createQuery(hql);
//		query.setString(0, name);
//		List<SysMetaTable> sysMetaTable=query.list();
		List<SysMetaTable> smForms=this.find(hql, name);
		
		if(smForms!=null&&smForms.size()>0){
			lastedSmForm=smForms.get(0);
		}
		return lastedSmForm;
	}
	
	/**
	 * 根据名称从数据库中查询所有版本的表单
	 * @param name
	 * @return
	 * cuigeqiang
	 */
	public List<SysMetaTable> getAllSmFormByName(String name){
		String hql="from SysMetaTable t where t.formName=? order by t.version desc";
		return this.find(hql, name);
	}
	
	/**
	 * 根据名称从数据库中查询最新的表单
	 * @param name
	 * @return
	 * cuigeqiang
	 */
	public SysMetaTable getLastedSmFormByTableName(String tableName){
		SysMetaTable lastedSmForm=null;
		
		String hql="from SysMetaTable t where t.tableName=? order by t.version desc";
		List<SysMetaTable> smForms=this.find(hql, tableName);
		
		if(smForms!=null&&smForms.size()>0){
			lastedSmForm=smForms.get(0);
		}
		return lastedSmForm;
	}
	
	/**
	 * 通过formId进行删除
	 * @param formId
	 * cuigeqiang
	 */
	public void deleteByFormId(String formId){
		String sql="DELETE FROM SYS_META_TABLE WHERE FORM_ID=?";
		Session session=this.getSession();
		SQLQuery query=session.createSQLQuery(sql);
		query.setString(0, formId);
		query.executeUpdate();
	}
	
	/**
	 * 通过表名进行删除
	 * @param tableName
	 */
	public void deleteByTableName(String tableName){
		String sql="DELETE FROM SYS_META_TABLE WHERE TABLE_NAME=?";
		Session session=this.getSession();
		SQLQuery query=session.createSQLQuery(sql);
		query.setString(0, tableName);
		query.executeUpdate();
	}
	
	/**
	 * 通过英文名和版本号查询对应的SMFORM
	 * @param smForms
	 * @return
	 * cuigeqiang
	 */
	public List<SysMetaTable> getFormsByTable(Table table){
		String hql="from SysMetaTable where formName=:name order by version desc";
		return this.getSession().createQuery(hql).setProperties(table).list();
	}
	
	/**
	 * 得到所有表单数据
	 * @return
	 * cuigeqiang
	 */
//	public List<SysMetaTable> getAllParentForms(){
//		String hql="from SysMetaTable where parentFormId is null order by version desc";
//		return this.getSession().createQuery(hql).list();
//	}
	
	/**
	 * 得到所有表单数据
	 * @return
	 * cuigeqiang
	 */
	public List<Map> getAllParentForms(){
		String hql="select id as id, tableName as tableName, tableChineseComments as tableChineseComments from SysMetaTable where parentFormName is null order by version desc";
		return this.getSession().createQuery(hql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
	}
	
	/**
	 *  得到所有智能表单数据
	 * @return
	 * cuigeqiang
	 */
	public List<SysMetaTable> getAllSmartForms(){
		String hql="from SysMetaTable where smartform = 'Y' order by version desc";
		return this.find(hql, new ArrayList());
	}
	
	/**
	 * 得到所有父表单名称
	 * @return
	 * cuigeqiang
	 */
	public List<Map> getAllParentSmFormName(){
		String hql="select formName as formName, tableChineseComments as tableChineseComments from SysMetaTable where smartform = 'Y' and parentFormName is null order by version desc";
		return this.getSession().createQuery(hql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
	}
}
